#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QString>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QFileDialog>
#include <QComboBox>
#include <QTextDocument>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::opendir()
{
    directory = QFileDialog::getExistingDirectory(this,
                                   tr("Укажите директорию"), QDir::currentPath());
    QDir qwerty(directory);
    QStringList filter;
    filter << "*.txt";
    QStringList info = qwerty.entryList(filter);

    //qDebug() << info;
    ui->comboBox->addItems(info);



    QFile inputFile(directory + "/" + ui->comboBox->currentText() );
          inputFile.open(QFile::ReadOnly | QFile::Text);

            QTextStream in(&inputFile);
            QString line = in.readAll();
            inputFile.close();

            ui->textEdit->setPlainText(line);



}

void MainWindow::savefile()
{
    QString nomeFile = QFileDialog::getSaveFileName(this, tr("Сохранить файл"), "",
                                            tr("Текстовый документ(*.txt)"));
   // qDebug() << "xxx" + nomeFile;
        if (nomeFile != "") {
            QFile file(nomeFile);

            if (file.open(QIODevice::ReadWrite)) {
         QTextStream stream(&file);
                stream << ui->textEdit->toPlainText();
                file.flush();
                file.close();
            }
            else {
                QMessageBox::critical(this, tr("Ошибка"), tr(""));
                return;
            }
        }


}



void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{

    QFile inputFile(directory +"/"+ ui->comboBox->currentText() );
          inputFile.open(QFile::ReadOnly | QFile::Text);

            QTextStream in(&inputFile);
          ui->textEdit->setText(in.readAll());
          //qDebug() << directory+inputFile.fileName();
            inputFile.close();
}
